package cn.online.zhuanshenben.service;

import cn.online.zhuanshenben.dao.AdvtiseMapper;
import cn.online.zhuanshenben.dao.SadminMapper;
import cn.online.zhuanshenben.entity.*;
import cn.online.zhuanshenben.util.MybatisUtil;
import cn.online.zhuanshenben.util.StrUtil;
import cn.online.zhuanshenben.vo.Page;
import com.github.pagehelper.PageHelper;
import org.apache.ibatis.session.SqlSession;

import java.util.Date;
import java.util.List;

/**
 * @author oldliu
 * @since 1.0
 */
public class AdvtiseService {
    public static final AdvtiseService ME = new AdvtiseService();

    public Page<Advtise> search(String title, String url, Date start, Date end, int page, int size) {
        SqlSession session = MybatisUtil.get();
        try {
            com.github.pagehelper.Page<Suser> p = PageHelper.startPage(page, size);

            AdvtiseMapper mapper = session.getMapper(AdvtiseMapper.class);
            AdvtiseExample admin=new AdvtiseExample();
            AdvtiseExample.Criteria cr = admin.createCriteria();
            if (!StrUtil.isBlank(title))
                cr.andTitleLike("%" + title + "%");
            if (!StrUtil.isBlank(url))
                cr.andUrlLike("%" + url + "%");
            if (!StrUtil.isBlank(start))
                cr.andStarttimeGreaterThanOrEqualTo(start);
            if (!StrUtil.isBlank(end))
                cr.andStarttimeLessThanOrEqualTo(end);
            List<Advtise> list = mapper.selectByExample(admin);
            return new Page<Advtise>(p.getTotal(), page, size, list);

        } finally {
            MybatisUtil.close(session);
        }
    }

}
